package com.task.web.controller.core;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.DataScriptTypeRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.DataScriptTypeService;
import com.task.core.vo.resp.DataScriptTypeDetailRespVo;
import com.task.core.vo.resp.DatasourceDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 脚本类型管理(DataScriptType)表控制层
 *
 * @author makejava
 * @since 2025-02-26 10:31:47
 */
@RestController
@RequestMapping("/dataScriptType")
public class DataScriptTypeController extends BaseController {
    @Resource
    private DataScriptTypeService dataScriptTypeService;

    @Resource
    private CheckService checkService;

    @Log(title = "数据脚本类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addScriptType")
    public AjaxResult addScriptType(@RequestBody DataScriptTypeRequestDTO request) {
        dataScriptTypeService.addScriptType(request);
        return success();
    }

    @GetMapping("/getScriptTypeForOption")
    @PreAuthorize("@ss.hasPermi('scriptType:option')")
    public TableDataInfo getScriptTypeForOption(DataScriptTypeRequestDTO request){
        return getDataTable(dataScriptTypeService.getScriptTypeForOption(request));
    }


    @PutMapping("/modifyScriptType")
    @Log(title = "数据脚本类型管理", businessType = BusinessType.UPDATE)
    public AjaxResult modifyScriptType(@RequestBody DataScriptTypeRequestDTO request){
        checkService.check("data_script_type", request.getCode());
        request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
        dataScriptTypeService.modifyScriptType(request);
        return success();
    }

    @DeleteMapping("/deleteScriptType/{scriptTypeCode}")
    @Log(title = "数据脚本类型管理", businessType = BusinessType.DELETE)
    public AjaxResult deleteScriptType(@PathVariable String scriptTypeCode){
        checkService.check("data_script_type", scriptTypeCode);
        dataScriptTypeService.deleteScriptType(scriptTypeCode,
                SecurityUtils.getLoginUser().getUserId());
        return success();
    }
    @GetMapping("/getScriptTypeByPage")
    @PreAuthorize("@ss.hasPermi('dataScriptType:list')")
    public TableDataInfo getScriptTypeByPage(DataScriptTypeRequestDTO request){
        startPage(request.getPageNum(),request.getPageSize());
        return getDataTable(dataScriptTypeService.getScriptTypeByPage(request));
    }

    @GetMapping ("/getScriptTypeDetail/{scriptTypeCode}")
    @PreAuthorize("@ss.hasPermi('system:dataScriptType:edit')")
    public AjaxResult getScriptTypeDetail(@PathVariable String scriptTypeCode) {
        checkService.check("data_script_type", scriptTypeCode);
        DataScriptTypeDetailRespVo dataScriptTypeDetailRespVo = dataScriptTypeService.getScriptTypeDetail(scriptTypeCode);
        return success(dataScriptTypeDetailRespVo);
    }
}

